﻿using GiveMeMenu.Common.LoggerService;
using GiveMeMenu.Interface.ServiceProvider;
using ObjectModel = GiveMeMenu.Interface.ServiceProvider.ObjectModel;
using GiveMeMenu.Models;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;

namespace GiveMeMenu.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class GMMController : Controller
    {
        public IGMMServiceProvider ServiceProvider { get; set; }
        public ILoggerService LoggerService { get; set; }

        public GMMController(IGMMServiceProvider serviceProvider, ILoggerService loggerService)
        {
            if (serviceProvider != null && loggerService != null)
            {
                ServiceProvider = serviceProvider;
                LoggerService = loggerService;
            }
            else
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// Updates the dish.
        /// </summary>
        /// <param name="dish">The dish.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddDish(ObjectModel.Dish dish)
        {
            bool result = ServiceProvider.AddDish(dish);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Adds the lookup table.
        /// </summary>
        /// <param name="lutable">The lutable.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLookupTable(ObjectModel.LuTable lutable)
        {
            bool result = ServiceProvider.AddluTable(lutable);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Adds the lookup type dish.
        /// </summary>
        /// <param name="dish">The dish.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddLookupTypeDish(ObjectModel.LuTypeDish dish)
        {
            bool result = ServiceProvider.AddluTypeDish(dish);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Adds the order information.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddOrderInformation(ObjectModel.OrderInformation order)
        {
            bool result = ServiceProvider.AddOrderInformation(order);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }


        /// <summary>
        /// Adds the order.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddOrder(ObjectModel.Order order)
        {
            bool result = ServiceProvider.AddOrder(order);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets all dishs.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllDishs()
        {
            IList<ObjectModel.Dish> result = ServiceProvider.GetAllDishes();
            IList<Dish> listResult = Mapper.Map<IList<ObjectModel.Dish>, IList<Dish>>(result);
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the dish today.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetDishToday()
        {
            IList<ObjectModel.Dish> result = ServiceProvider.GetDishToday();
            IList<Dish> listResult = Mapper.Map<IList<ObjectModel.Dish>, IList<Dish>>(result);
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the lookup table.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetLookupTable()
        {
            IList<ObjectModel.LuTable> result = ServiceProvider.GetluTables();
            IList<LookupTable> listResult = Mapper.Map<IList<ObjectModel.LuTable>, IList<LookupTable>>(result);
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the lookup type dish.
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetLookupTypeDish()
        {
            IList<ObjectModel.LuTypeDish> result = ServiceProvider.GetluTypeDish();
            IList<LookupTypeDish> listResult = Mapper.Map<IList<ObjectModel.LuTypeDish>, IList<LookupTypeDish>>(result);
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Gets the order information.
        /// </summary>
        /// <param name="orderOId">The order O id.</param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetOrderInformation(int orderOId)
        {
            IList<ObjectModel.OrderInformation> result = ServiceProvider.GetOrderInformationByOrderId(orderOId);
            IList<OrderInformation> listResult = Mapper.Map<IList<ObjectModel.OrderInformation>, IList<OrderInformation>>(result);
            return Json(listResult, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Removes the order.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveOrder(int OId)
        {
            bool result = ServiceProvider.RemoveOrder(OId);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Removes the order information.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveOrderInformation(int OId)
        {
            bool result = ServiceProvider.RemoveOrderInformation(OId);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Removes the dish.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveDish(int OId)
        {
            bool result = ServiceProvider.RemoveDish(OId);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Removes the lookup table.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveLookupTable(int OId)
        {
            bool result = ServiceProvider.RemoveluTable(OId);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Removes the lookup type dish.
        /// </summary>
        /// <param name="OId">The O id.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult RemoveLookupTypeDish(int OId)
        {
            bool result = ServiceProvider.RemoveluTypeDish(OId);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Updates the dish.
        /// </summary>
        /// <param name="dish">The dish.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateDish(ObjectModel.Dish dish)
        {
            bool result = ServiceProvider.UpdateDish(dish);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Updates the lookup table.
        /// </summary>
        /// <param name="lutable">The lutable.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateLookupTable(ObjectModel.LuTable lutable)
        {
            bool result = ServiceProvider.UpdateluTable(lutable);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Updates the lookup type dish.
        /// </summary>
        /// <param name="lutypeDish">The lutype dish.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateLookupTypeDish(ObjectModel.LuTypeDish lutypeDish)
        {
            bool result = ServiceProvider.UpdateluTypeDish(lutypeDish);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// Updates the order.
        /// </summary>
        /// <param name="order">The order.</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateOrder(ObjectModel.Order order)
        {
            bool result = ServiceProvider.UpdateOrder(order);

            if (result)
            {
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            return Json(result, JsonRequestBehavior.AllowGet);
        }
    }
}
